import { request } from 'umi';

// Role API endpoints
const API_PREFIX = 'http://localhost:8102/api';

/**
 * Role interface matching the backend TRoleDTO
 */
export interface RoleData {
  id?: number;
  name: string;
  description?: string;
  status?: boolean;
  createdAt?: string;
}

/**
 * API response interface matching the backend ApiResponse
 */
export interface ApiResponse<T> {
  success: boolean;
  message: string;
  data: T;
  code: number;
}

/**
 * Get all roles
 */
export async function getRoles() {
  return request<ApiResponse<RoleData[]>>(`${API_PREFIX}/roles`, {
    method: 'GET',
  });
}

/**
 * Get active roles
 */
export async function getActiveRoles() {
  return request<ApiResponse<RoleData[]>>(`${API_PREFIX}/roles/active`, {
    method: 'GET',
  });
}

/**
 * Get role by ID
 */
export async function getRole(id: number) {
  return request<ApiResponse<RoleData>>(`${API_PREFIX}/roles/${id}`, {
    method: 'GET',
  });
}

/**
 * Create new role
 */
export async function createRole(data: RoleData) {
  return request<ApiResponse<RoleData>>(`${API_PREFIX}/roles`, {
    method: 'POST',
    data,
  });
}

/**
 * Update role
 */
export async function updateRole(id: number, data: RoleData) {
  return request<ApiResponse<RoleData>>(`${API_PREFIX}/roles/${id}`, {
    method: 'PUT',
    data,
  });
}

/**
 * Update role status
 */
export async function updateRoleStatus(id: number, status: boolean) {
  return request<ApiResponse<RoleData>>(`${API_PREFIX}/roles/${id}/status`, {
    method: 'PATCH',
    params: { status },
  });
}

/**
 * Delete role
 */
export async function deleteRole(id: number) {
  return request<ApiResponse<void>>(`${API_PREFIX}/roles/${id}`, {
    method: 'DELETE',
  });
}
